import 'package:aero_frisbee/gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:svgaplayer_flutter/parser.dart';
import 'package:svgaplayer_flutter/player.dart';

class AfRecordView extends StatefulWidget {
  const AfRecordView({Key? key}) : super(key: key);
  @override
  AfRecordViewState createState() => AfRecordViewState();
}

class AfRecordViewState extends State<AfRecordView>
    with SingleTickerProviderStateMixin {
  SVGAAnimationController? animationController;
  @override
  void initState() {
    super.initState();
    animationController = SVGAAnimationController(vsync: this);
    _loadAnimation();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    animationController?.dispose();
    animationController = null;
    super.dispose();
  }

  void _loadAnimation() async {
    final videoItem =
        await SVGAParser.shared.decodeFromAssets(Assets.svga.record);

    if (mounted) {
      setState(() {
        animationController?.videoItem = videoItem;
        _playAnimation();
      });
    }
  }

  void _playAnimation() {
    if (animationController?.isCompleted == true) {
      animationController?.reset();
    }
    animationController?.repeat(); // or animationController.forward();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 131,
      child: SVGAImage(
        animationController!,
        fit: BoxFit.contain,
        clearsAfterStop: false,
        allowDrawingOverflow: true,
        filterQuality: FilterQuality.low,
      ),
    );
  }
}
